﻿
angular.module('SLG')
  .controller("createKingdomController", function ($scope, $state, $http, $rootScope, $http) {

      $scope.chooseRaces = true;
      $scope.chooseKings = false;
      $scope.choosekingName = false;

      $scope.Races = [];
      $scope.Kings = [];
      $scope.kingName = "";

      //保存需要传递到后台的数据json
      var createKingdomInfo = {raceId:"",kingId:"",kingName:""};

      //checkbox事件处理
      function resetChecked(items) {
          for (var i = 0; i < items.length; i++) {
              items[i].checked = false;
          }
      }
      $scope.chooseItem = function (item, items) {
          resetChecked(items);
          item.checked == false ? item.checked = true : item.checked = false;
      }

      //获取选中项
      function getChooseItem(items) {
          var item = null;
          for (var i = 0; i < items.length; i++) {
              if (items[i].checked) {
                  item = items[i];
                  break;
              }
          }
          return item;
      }

      /**********************选择种族方法  begin**************************/
      function getRaces() {
          var url = "../../Api.ashx?sys:sno=BD&sys:act=GetRaces";

          $http.get(url).success(function (data) {
              if (data.IsSuccess) {
                  $scope.Races = data.Data.Items;
                  resetChecked($scope.Races);
              }
          })
      }

      $scope.nextStepOne = function () {
          //获取国王
          var race = getChooseItem($scope.Races);
          if (race == null) {
              alert("请选择一个种族!");
              return;
          }
          else {
              getKings(race.Id);
          }
          $scope.chooseRaces = false;
          $scope.chooseKings = true;
          $scope.choosekingName = false;
      }

      //获取种族
      getRaces();
      /**********************选择种族方法   end**************************/


      /**********************选择国王头像方法  begin**************************/
      function getKings(raceId) {
          var url = "../../Api.ashx?sys:sno=BD&sys:act=GetKings&Race="+raceId;
          $http.get(url).success(function (data) {
              if (data.IsSuccess) {
                  $scope.Kings = data.Data.Items;
                  resetChecked($scope.Kings);
              }
          })
      }

      $scope.nextStepTwo = function () {
          $scope.chooseRaces = false;
          $scope.chooseKings = false;
          $scope.choosekingName = true;
      }
      /**********************选择国王头像方法   end**************************/


      /**********************创建王国  begin**************************/

      $scope.createKing = function () {
          var race = getChooseItem($scope.Races);
          var king = getChooseItem($scope.Kings);
          var kingName = $scope.kingName;
          var params = { "Race": race.Id, "King": king.Id, Name:kingName };
          var url = "../../Api.ashx?sys:sno=Kingdom&sys:act=Create";
          $http.post(url, params).success(function (data) {
            
          })
      }
      //{KingdomId: "3cb88270-fa9c-4773-a220-66832cc8bd97", KingdomName: "Damon's Kingdom", KingId: "9942b872-505e-4366-8ae1-56aabdcb53d6", KingName: "Damon", Race: 2}

      /**********************创建王国  end****************************/
  })